<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="core.*,util.*" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    form > div { margin-top: 20px; }
</style>
</head>
<%
    request.setCharacterEncoding("UTF-8");
    String loginId = request.getParameter("loginId");
    String email = request.getParameter("email");
    String errMsg = null, infoMsg = null;

    if (loginId == null) loginId = "";
    if (email == null) email = "";

    if (request.getMethod().equals("POST")) {
        User user = UserDAO.selectByLoginId(loginId);
        if (user != null) {
            if (user.getEmail().equals(email)) {
                String passwd = UserService.makeTempPassword();
                UserDAO.updatePasswd(user.getId(), UserService.encryptPasswd(passwd));
                String url = UrlUtil.getFullURL(request);
                SMTP.sendGMail(email, "임시 비밀번호 전송", url + " 임시 비밀번호: " + passwd);
                infoMsg = "임시 비밀번호를 이메일로 전송하였습니다";
            }
            else errMsg = "이메일이 일치하지 않습니다";
        }
        else errMsg = "로그인 아이디가 잘못되었습니다";
    }
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container main">

    <h1>비밀번호 찾기</h1>
    <hr />

    <form method="post">
        <div>아이디</div>
        <input type="text" name="loginId" value="<%= loginId %>" />

        <div>이메일</div>
        <input type="text" name="email" value="<%= email %>" />

        <div>
            <button type="submit" class="btn btn-primary">
                <i class="icon-envelope icon-white"></i> 임시 비밀번호 전송
            </button>
        </div>
    </form>
    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>
    <% if (infoMsg != null) { %>
        <div class="alert alert-info"><%= infoMsg %></div>
    <% } %>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>